package fudan.se.project.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import fudan.se.project.domain.conference.role.Author;
import fudan.se.project.domain.conference.role.Chair;
import fudan.se.project.domain.conference.role.PcMember;
import fudan.se.project.domain.sysrole.SystemAuthority;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author Gao Longchao
 * <p>
 */
@Entity
@Getter
@Setter
@NoArgsConstructor
public class User implements UserDetails {

    private static final long serialVersionUID = -6140085056226164016L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique = true)
    private String username;

    private String realname;

    @JsonIgnore
    private String password;

    @Column(unique = true)
    private String email;

    private String organization;
    private String region;

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user")
    private Set<Chair> chairs = new HashSet<>();

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user")
    private Set<PcMember> pcMembers = new HashSet<>();

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user")
    private Set<Author> authors = new HashSet<>();

    public User(
            String username, String realname, String password,
            String email, String organization, String region
    ) {
        this.username = username;
        this.realname = realname;

        this.password = password;

        this.email = email;
        this.organization = organization;
        this.region = region;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    private Set<SystemAuthority> systemAuthorities = new HashSet<>();


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return systemAuthorities;
    }

    @Override
    public String getPassword() {
        return password;
    }


    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof User) {
            if (this.username == null) return null == ((User) obj).getUsername();
            return this.username.equals(((User) obj).getUsername());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(getUsername());
    }
}
